package com.ffsky.template.demo.app;

import android.os.Bundle;
import android.webkit.JavascriptInterface;

import androidx.appcompat.app.AppCompatActivity;

import com.ffsky.dsbridge.demo.R;
import com.ffsky.template.demo.ajax.AjaxHandler;
import com.gitee.hljdrl.dsbridge.DSWebView;
import com.gitee.hljdrl.dsbridge.basis.DSCallback;
import com.gitee.hljdrl.dsbridge.jsapi.DSPlugin;

import org.json.JSONObject;


public class FlyIoTestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wrok_with_flyio_test);
        DSWebView dWebView = findViewById(R.id.webview);
//        dWebView.addJavascriptObject(new Object(){
//
//            /**
//             * Note: This method is for Fly.js
//             * In browser, Ajax requests are sent by browser, but Fly can
//             * redirect requests to native, more about Fly see  https://github.com/wendux/fly
//             * @param requestData passed by fly.js, more detail reference https://wendux.github.io/dist/#/doc/flyio-en/native
//             * @param handler
//             */
//            @JavascriptInterface
//            public void onAjaxRequest(Object requestData, DsCallback handler){
//                // Handle ajax request redirected by Fly
//                AjaxHandler.onAjaxRequest((JSONObject)requestData,handler);
//            }
//
//        },null);

        dWebView.addDSPlugin(null, new DSPlugin() {
            @JavascriptInterface
            public void onAjaxRequest(Object requestData, DSCallback handler) {
                // Handle ajax request redirected by Fly
                AjaxHandler.onAjaxRequest((JSONObject) requestData, handler);
            }
        });
        dWebView.loadUrl("file:///android_asset/fly.html");
    }
}
